# Kestrel: An XMPP-based Many-Task Computing Scheduler
# Author: Lance Stout <lancestout@gmail.com>
#
# Copyright 2009-2010 Clemson University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


class Interface(object):
    def __init__(self, kernel):
        """
        The system interface issues XMPP presence notifications
        whenever the system's state is changed.

        Events listened for by this module:
          system/available
          system/busy
          system/access/request
          system/access/approve

        Events raised by this module:
          presence/send
          system/access/requested
          system/access/approved
        """
        self.kernel = kernel
        self.events = {'system/available': self.available,
                       'system/busy': self.busy,
                       'presence/subscribe': self.access_requested,
                       'presence/subscribed': self.access_approved,
                       'system/access/request': self.request_access,
                       'system/access/approve': self.approve_access}
        self.kernel.register(self)

    def available(self, data=None):
        """Set and broadcast the system status as available."""
        self.kernel.set_config('system/status', 'available')
        self.kernel.event('presence/send', 'available')

    def busy(self, data=None):
        """Set and broadcast the system status as busy."""
        self.kernel.set_config('system/status', 'busy')

        self.kernel.event('presence/send', 'busy')

    def request_access(self, entity):
        """
        Request access to another agent, such as an agent with a
        manager role.
        """
        self.kernel.event('presence/send', {'to': entity,
                                            'type': 'subscribe'})

    def access_approved(self, entity):
        """
        Notification that a previous request for access to an agent
        has been approved.
        """
        self.kernel.event('system/access/approved', entity)

    def access_requested(self, entity):
        """
        Notification that an agent has requested access to this agent.
        """
        self.kernel.event('system/access/requested', entity)

    def approve_access(self, entity):
        """
        Send a notification to an agent that its previous request for
        access has been authorized.
        """
        self.kernel.event('presence/send', {'to': entity,
                                            'type': 'subscribed'})
